#!/bin/bash -x

# Check if an argument was provided
if [ -z "$1" ]; then
  echo "Must specify document to run"
  exit 1
fi

# Pre-initialize variables
filepath=""
# cuda supports padding, so no need to replace quantization for now.  
# otherwise add: 'cuda.json:cuda-32.json' to replace rules
parameters="--replace llama3:stories15M,-l3:-l2,mobile.json:mobile-32.json --suppress huggingface-cli,HF_TOKEN"
script_name="./run-${1}.sh"  # Dynamically initialize script name

# Use a case statement to handle the $1 argument
case "$1" in
  "readme")
    filepath="README.md"
    parameters="--replace llama3.1:stories15M,-l3:-l2,mobile.json:mobile-32.json --suppress huggingface-cli,HF_TOKEN"
    ;;
  "quantization")
    filepath="docs/quantization.md"
    ;;
  "gguf")
    filepath="docs/GGUF.md"
    ;;
  "advanced")
    filepath="docs/ADVANCED-USERS.md"
    ;;
  "evaluation")
    filepath="torchchat/utils/docs/evaluation.md"
    ;;
  "multimodal")
    filepath="docs/multimodal.md"
    parameters=""  # Clear parameters
    ;;
  "native")
    filepath="docs/native-execution.md"
    parameters=""  # Clear parameters
    ;;
  "distributed")
    filepath="docs/distributed.md"
    parameters="--replace llama3.1:stories110M,-l3:-l2 --suppress huggingface-cli,HF_TOKEN"  # Use stories110M to avoid need for authentication
    ;;
  "local")
    filepath="docs/local-model.md"
    parameters=""  # Clear parameters
    ;;

  *)
    echo "Unknown option: $1"
    exit 1
    ;;
esac

# Generate the script
echo "::group::Create script to run $1"
python3 torchchat/utils/scripts/updown.py --file "$filepath" $parameters > "$script_name"
# if something happened to updown processor, and it did not error out, fail with an exit 1
echo "exit 1" >> "$script_name"
echo "::endgroup::"

# Run the script
echo "::group::Run $1"
echo "*******************************************"
cat "$script_name"
echo "*******************************************"
set -x
. "$script_name"
echo "::endgroup::"
